package com.huawei.rbac.test;

import java.lang.reflect.Constructor;

public class Singleton_hunger {

    //  饿汉式单例在类初始化的时候完成实例化
	private static  final  Singleton_hunger mIntance = new Singleton_hunger();

	// 私有构造函数
	private Singleton_hunger(){
		
	}
	
	public static Singleton_hunger getInstance() {
		return mIntance;
	}

	public static void main(String[] args) throws Exception{

		// 反射创建对象会破怪单例
		Singleton_hunger instance2 = Singleton_hunger.class.newInstance();
		Singleton_hunger instance3 = (Singleton_hunger) Class.forName("com.huawei.rbac.test.Singleton_hunger").newInstance();
		// 获取私有构造函数
		Constructor<? extends Singleton_hunger> constructor = instance2.getClass().getDeclaredConstructor();
		// 设置暴力反射访问
		constructor.setAccessible(true);
		Singleton_hunger instance4 = constructor.newInstance();

		Singleton_hunger instance = Singleton_hunger.getInstance();
        Singleton_hunger instance1 = Singleton_hunger.getInstance();
        System.out.println(instance == instance1);
        System.out.println(instance1 == instance2);
        System.out.println(instance2 == instance3);
        System.out.println(instance2 == instance4);
    }

}